package cn.stylefeng.guns.modular.productfunction.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.productfunction.entity.ShiAdditionalFunction;
import cn.stylefeng.guns.modular.productfunction.mapper.ShiAdditionalFunctionMapper;
import cn.stylefeng.guns.modular.productfunction.model.params.ShiAdditionalFunctionParam;
import cn.stylefeng.guns.modular.productfunction.model.result.ShiAdditionalFunctionResult;
import cn.stylefeng.guns.modular.productfunction.service.ShiAdditionalFunctionService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 * 附加功能表
 * @author 石博士
 * @since 2019-07-05
 */
@Service
public class ShiAdditionalFunctionServiceImpl extends ServiceImpl<ShiAdditionalFunctionMapper, ShiAdditionalFunction> implements ShiAdditionalFunctionService {

    @Override
    public ShiAdditionalFunction add(ShiAdditionalFunctionParam param){
        ShiAdditionalFunction entity = getEntity(param);
        this.save(entity);
        return entity;
    }

    @Override
    public void delete(ShiAdditionalFunctionParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ShiAdditionalFunctionParam param){
        ShiAdditionalFunction oldEntity = getOldEntity(param);
        ShiAdditionalFunction newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ShiAdditionalFunctionResult findBySpec(ShiAdditionalFunctionParam param){
        return null;
    }

    @Override
    public List<ShiAdditionalFunctionResult> findListBySpec(ShiAdditionalFunctionParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ShiAdditionalFunctionParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ShiAdditionalFunctionParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ShiAdditionalFunction getOldEntity(ShiAdditionalFunctionParam param) {
        return this.getById(getKey(param));
    }

    private ShiAdditionalFunction getEntity(ShiAdditionalFunctionParam param) {
        ShiAdditionalFunction entity = new ShiAdditionalFunction();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
